<?php
namespace Common\Controller;
use Common\Model\InfoContentModel;
use Common\Model\NavigationModel;
use Think\Controller;
class BehaviorController extends Controller {

    public function __construct(){
        parent::__construct();
        $this->LoadConfig();
        $this->addConfig();
        define('TMPL_PATH',__ROOT__.'./Template/');		//定义主题模板目录
        define('THEME_PATH','./Template/'.C('DEFAULT_THEME'));//当前模板路径
        $TMPL_PARSE_STRING = C('TMPL_PARSE_STRING');
        $TMPL_PARSE_STRING['__THEME_PATH__'] = __ROOT__.'/Template/'. C('DEFAULT_THEME').'/';
        C('TMPL_PARSE_STRING',$TMPL_PARSE_STRING);

        $this->initCookDisplayType();
        $this->getNav();

    }

    public function addConfig(){
        foreach($this->config  as $k => $v){
            C($k,$v);
        }
        $host='http://'.$_SERVER['SERVER_NAME'];
        C('CURRENT_HOST',$host);
        return true;
    }

    public function getNav(){
        $model = new NavigationModel();
        $menu = $model->getMenuList();
        $this->assign('menu',$menu);
    }

    public function LoadConfig(){
        $config = M('sysconfig')->where("groupid = 1")->field("varname,value")->select();
        $config = array_column($config,'value','varname');
        $this->config = $config;
        $this->assign('config',$config);
    }


    public function PageShow($count,$list = '/list/',$limit = null){
        $limit = $limit == null ? C('PAGE_SIZE') : $limit;
        $Page = new \Org\Util\Page($count,$limit);
        $Page->setConfig('header','<div class="pageNum">第%NOW_PAGE%/%TOTAL_PAGE%页</div>');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','末页');
        $Page->setConfig('first','首页');
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $show = $Page->show($list);// 分页显示输出
        return $show;
    }

    public function initCookDisplayType(){
        if(isset($_COOKIE['displaytype'])){
            $displaytype = (int)$_COOKIE['displaytype'];
        }else{
            $displaytype = 0;
        }
        $this->displaytype = $displaytype;
        return true;
    }

    public function IsLogin($jump=true,$id=null){
        $id = $id == null ? 'uid' : $id;
        $key = sprintf('%s_%s',C('COOK_LOGIN_KEY'),$id);
        $userCookie = cookie($key) ? cookie($key) : '';

        if($userCookie){
            $sValue = isset($_SESSION[$userCookie]) && !empty($_SESSION[$userCookie]) ? $_SESSION[$userCookie] :"";
            if($sValue){
                $userCookieTemp = explode('_',$userCookie);
                $sValueTemp = explode('_',$sValue);

                if(isset($userCookieTemp[3]) && $userCookieTemp[3] == 'qq'){
                    if($userCookieTemp[2] == md5($sValueTemp[2])){
                        return $userCookieTemp;
                    };
                }
                if(count($sValueTemp) >= 3 && count($userCookieTemp) >= 3){
                    if($userCookieTemp[0] == $sValueTemp[0]
                        && $userCookieTemp[1] == $sValueTemp[1]
                        && $userCookieTemp[2] == md5($sValueTemp[1].$sValueTemp[2].C('PASSWORD_KEY'))){
                        return $userCookieTemp;
                    }
                }
            }
        }
        if($jump){
            if($id == 'aid'){
                $url = U("admin/login/index");
            }else{
                $url = U("user/index/login");
            }
            $this->error("登录超时,请重新登录",$url);
        }else{
            return false;
        }
    }

    public function getinfoInInfoId($idArr){
        $infoModel = new InfoContentModel();
        $data = $infoModel->getinfoInInfoId($idArr);
        return $data;
    }

}